(define *kR:keys* (list))

(define (keys:reset)
   (set! *kR:keys* (list)))

(define-macro (key key-code . body)
   `(set-obj-for-key! ,key-code (lambda (pressed) ,@body) *kR:keys*))

(define-macro (key-down key-code . body)
    `(key ,key-code
        (if pressed
            ,@body)))

(define-macro (key-up key-code . body)
   `(key ,key-code
        (if (not pressed)
            ,@body)))

;old functions for assigning toogle states to a key
(define *kR:key-toggle-states* (list))
(define (key-toggle key-code start fn) 
   (set-obj-for-key! key-code start *kR:key-toggle-states*)
   (key key-code
        ;only on key-down
        (if pressed
            (let
               ((cell (assoc key-code *kR:key-toggle-states*)))
                (set-cdr! cell (not (cdr cell)))
                (fn (cdr cell))))))

(define (key-midi-bypass key-code _instr start)
   (instr:set _instr "bypass-midi" start)
   (instr:update-midi)
   (key-toggle key-code start (lambda (state) 
                           ;the instrument is allready loaded
                           (instr:set _instr "bypass-midi" state)
                           (instr:update-midi))))